package com.cn.wanxi.servlet.manage.user;

import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UserAdd")
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
////        0.乱码解决
//        req.setCharacterEncoding("UTF-8");//请求编码格式为utf-8
//        resp.setCharacterEncoding("UTF-8");//响应编码格式为utf-8
//        resp.setContentType("text/json;charset=utf-8");//解决传值乱码
//        1.拿到页面的值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        2.封装对象
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
//        3.调用逻辑层
        IUserService iUserService=new UserServiceImpl();
        int num=iUserService.add(userModel);
//        4，返回数据
        JSONArray jsonArray=JSONArray.fromObject(num);
        resp.getWriter().println(jsonArray);
    }
}
